#pragma once

#include "iostream"
#include "vector"
#include "algorithm"
#include "TypeDefin.h"

using namespace std;

/*HJJ QQ479287006
 *输入两个链表，找出它们的第一个公共节点。

如下面的两个链表：
 * */

int getLen(ListNode *head) {
    if (!head)

        return 0;

    int num = 0;

    while (head) {
        head = head->next;
        ++num;
    }

    return num;

}

ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {

    int lena = getLen(headA);
    int lenb = getLen(headB);
    if (lena - lenb > 0) {
        int t = lena - lenb;
        while (t--) {
            // if(!headA) return nullptr;

            headA = headA->next;
        }

    } else {
        int t = lenb - lena;
        while (t--) {
            //if(!headB) return nullptr;
            headB = headB->next;
        }

    }
    while (headA != headB) {
        headA = headA->next;
        headB = headB->next;
    }

    return headB;
}